package com.example.g5project.service.impl;

import com.example.g5project.entity.Consultant;
import com.example.g5project.entity.frontEndObject.ConsultantIDwithConsultNumber;
import com.example.g5project.entity.frontEndObject.ConsultantIDwithMonthGrade;
import com.example.g5project.entity.relation.ConsultInfo;
import com.example.g5project.exception.EntityNotExistException;
import com.example.g5project.repository.repo.AdministratorRepo;
import com.example.g5project.repository.repo.ConsultInfoRepo;
import com.example.g5project.repository.repo.ConsultantRepo;
import com.example.g5project.service.ConsultantService;
import com.example.g5project.tools.EntityUtils;
import org.assertj.core.util.Preconditions;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * @author Hu Zirui
 * @version 1.0.0
 * @ClassName ConsultantServiceImpl.java
 * @Description TODO
 * @createTime 2022年03月09日 13:18:00
 */
@Service
@Transactional
public class ConsultantServiceImpl implements ConsultantService {
    private final ConsultantRepo consultantRepo;
    private final AdministratorRepo administratorRepo;
    private final ConsultInfoRepo consultInfoRepo;

    public ConsultantServiceImpl(ConsultantRepo consultantRepo, AdministratorRepo administratorRepo, ConsultInfoRepo consultInfoRepo) {
        this.consultantRepo = consultantRepo;
        this.administratorRepo = administratorRepo;
        this.consultInfoRepo = consultInfoRepo;
    }

    @Override
    public Consultant getConsultantByID(Integer id) throws EntityNotExistException {
        Preconditions.checkNotNull(id, "未输入咨询师ID");
        Optional<Consultant> opt = consultantRepo.findById(id);
        if (opt.isPresent())
            return opt.get();
        throw new EntityNotExistException("Consultant");
    }

    @Override
    public List<Consultant> getConsultantByIds(Collection<Integer> ids) {
        return consultantRepo.findByIds(ids);
    }

    @Override
    public Consultant getCounsultantByName(String name) {
        Preconditions.checkNotNull(name, "未输入咨询师姓名");
        Optional<Consultant> opt = consultantRepo.findByName(name);
        return opt.orElse(null);
    }

    @Override
    public void addConsultant(Consultant consultant) {
        Preconditions.checkNotNull(consultant, "未输入咨询师信息");
        consultantRepo.add(consultant);
    }

    @Override
    public void deleteConsultantByID(Integer id) {
        Preconditions.checkNotNull(id, "未输入咨询师ID");
        administratorRepo.deleteConsultantFromScheduleByIDAll(id);
        administratorRepo.deleteConsultantFromMessageByIDAll(id);
        administratorRepo.deleteConsultantFromBindByIDAll(id);
        consultantRepo.deleteById(id);
    }

    @Override
    public void deleteConsultantByName(String name) {
        Preconditions.checkNotNull(name, "未输入咨询师姓名");
        consultantRepo.deleteByName(name);
    }

    @Override
    public void updateConsultantByID(Integer id, Consultant consultant) throws EntityNotExistException {
        Preconditions.checkNotNull(consultant, "未输入咨询师信息");
        Preconditions.checkNotNull(id, "未输入咨询师ID");
        consultantRepo.updateById(id, consultant);
    }

    @Override
    public ArrayList<Consultant> getAllConsultant() {
        return consultantRepo.findAll();
    }

    @Override
    public Integer getTodayConsultNumber(Integer id) {
        Preconditions.checkNotNull(id, "未输入咨询师ID");
        return consultantRepo.getTodayConsultNumber(id);
    }

    @Override
    public String getTodayConsultTime(Integer id) {
        Preconditions.checkNotNull(id, "未输入咨询师ID");
        return consultantRepo.getTodayConsultTime(id);
    }

    @Override
    public ArrayList<Consultant> getAllAliveConsultant() {
        return consultantRepo.getAllAliveConsultant();
    }

    @Override
    public Integer getAllConsultSessionNumber() {
        return consultantRepo.getAllConsultSessionNumber();
    }

    @Override
    public ArrayList<ConsultantIDwithConsultNumber> getFirstKrankConsultantByConsultNumber(int k, int month) {
        Preconditions.checkArgument(k > 0, "应该至少返回大于0的数量");
        Preconditions.checkArgument(month >= 1 && month <= 12, "月份不合理");
        return consultantRepo.getFirstKrankConsultantByConsultNumber(k, month);
    }

    @Override
    public ArrayList<ConsultantIDwithMonthGrade> getFirstKrankConsultantByMonthsGrade(int k, int month) {
        Preconditions.checkArgument(k > 0, "应该至少返回大于0的数量");
        Preconditions.checkArgument(month >= 1 && month <= 12, "月份不合理");
        return consultantRepo.getFirstKrankConsultantByMonthsGrade(k, month);
    }

    @Override
    public void addAliveSessionNumber(Integer id) {
        Preconditions.checkNotNull(id, "未输入咨询师ID");
        consultantRepo.addAliveSessionNumber(id);
    }

    @Override
    public void addTodaySessionNumber(Integer id) {
        Preconditions.checkNotNull(id, "未输入咨询师ID");
        consultantRepo.addTodaySessionNumber(id);
    }

    @Override
    public ArrayList<String> getScheduleByMonth(String name, String month) {
        return consultantRepo.getScheduleByMonth(name, month);
    }

    @Override
    public Integer getGradeByName(String name) {
        return consultantRepo.getGradeByName(name);
    }


    @Override
    public ArrayList<ConsultInfo> getAllConsultMessage(String name) {
        return consultInfoRepo.getAllConsultMessageByConsultName(name);
    }

    @Override
    public void setStatus(String name) {
        consultantRepo.setStatus(name);
    }

    @Override
    public void increAllConsultNumber(String name) {
        consultantRepo.increAllConsultNumber(name);
    }

    @Override
    public void incretodayConsultNumber(String name) {
        consultantRepo.incretodayConsultNumber(name);
    }

    @Override
    public void updateHoldTimeByName(String name, String hold_time) {
        consultantRepo.updateallHoldTimeByName(name, hold_time);
        consultantRepo.updatetodayHoldTimeByName(name, hold_time);
    }

    @Override
    public void increTodayConsultTime(String name, String time) {
        Optional<Consultant> consultant = consultantRepo.findByName(name);
        if (consultant.isPresent()) {
            String hold_time = String.valueOf(consultant.get().getToday_consult_time());
            if (hold_time == null || hold_time.equals("null"))
                hold_time = "00:00:00";
            System.out.println(hold_time);
            EntityUtils entityUtils = new EntityUtils();
            String new_hold_time = entityUtils.transformDate(hold_time, time);
            consultantRepo.updatetodayHoldTimeByName(name, new_hold_time);
        }
    }

    @Override
    public void increAllConsultTime(String name, String time) {
        Optional<Consultant> consultant = consultantRepo.findByName(name);
        if (consultant.isPresent()) {
            String hold_time = String.valueOf(consultant.get().getToday_consult_time());
            if (hold_time == null || hold_time.equals("null"))
                hold_time = "00:00:00";
            EntityUtils entityUtils = new EntityUtils();
            String new_hold_time = entityUtils.transformDate(hold_time, time);
            consultantRepo.updateallHoldTimeByName(name, new_hold_time);
        }
    }

    @Override
    public void addAliveSessionNumberByName(String name) {
        consultantRepo.addAliveSessionNumberByName(name);
    }

    @Override
    public void minusAliveSessionNumber(Integer id) {
        consultantRepo.minusAliveSessionNumber(id);
    }

    @Override
    public void minusAliveSessionNumberByName(String name) {
        consultantRepo.minusAliveSessionNumberByName(name);
    }


}
